<template>
  <div class="app-container">
    <div style="width: 35%;">
      <el-alert title="奖金池配置" type="success" show-icon :closable="false"></el-alert>
      <!-- description="文字说明文字说明文字说明文字说明文字说明文字说明"  -->
      <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="分红周期" prop="type" style="margin-top: 20px;">
            <el-radio-group ref="radio" v-model="ruleForm.type">
              <el-radio :label="1">周</el-radio>
              <el-radio :label="2">月</el-radio>
              <el-radio :label="3">年</el-radio>
            </el-radio-group>
          </el-form-item>  
          <el-form-item label="分红时间" prop="count">
            <el-input v-model="ruleForm.count" type="number" style="width: 100px;"></el-input>
            <span style="margin-left: 10px;">{{ ruleForm.type == 1 ? '周': (ruleForm.type == 2 ? '月':'年') }}</span>
          </el-form-item>
          <el-form-item label="分红比例" prop="ratio">
            <el-input v-model="ruleForm.ratio" :min="1" :max="100" type="number" style="width: 100px;" @blur="changeRatio"></el-input>
            <span style="margin-left: 10px;">‱</span>
          </el-form-item>
          <el-form-item label="分红等级" prop="levelId">
            <el-select
              v-model="ruleForm.levelId"
              placeholder="请选择用户等级"
              clearable
              disabled
            >
              <el-option 
                v-for="dict in  dict.type.user_level_list"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">提交</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
  
  <script>
import { getConfig, changeConfig } from "@/api/bonusPool/bonusPool";
import { Debounce } from '@/utils/tools.js'
export default {
  name: "productCate",
  dicts: ["user_level_list","user_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      areaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        areaName: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        areaName: [
          { required: true, message: "请输入区域名称", trigger: "blur" },
        ],
      },
      ruleForm:{
        type: null,
        count: null,
        levelId: null,
        ratio: null,
      },
      configInfo: {},
    };
  },
  created() {
    this.getConfigInfo();
  },
  mounted() {
    this.$refs.radio.$children.forEach(item =>{
      item.$refs.radio.removeAttribute("aria-hidden")
    })
  },
  methods: {
    changeRatio(e){
      if(this.ruleForm.ratio > 100){
        this.ruleForm.ratio = 100
      }else if(this.ruleForm.ratio < 1){
        this.ruleForm.ratio = 1
      }
    },
    // 获取配置
    getConfigInfo(){
      getConfig('bonus.allot.time').then(res => {
        if(res.code == 200){
          let data = res.msg
          data=data.replace(/\\r\\n/g,"");
          data = JSON.parse(data)
          data.ratio = data.ratio * 10000
          data.levelId = data.levelId + ''
          this.ruleForm = data
        }
      })
    },
    /** 提交 */
    handleQuery() {
      if(this.ruleForm.count){
        this.ruleForm.count = parseInt(this.ruleForm.count)
      }
      let dataFrom = JSON.parse(JSON.stringify(this.ruleForm))
      dataFrom.ratio = dataFrom.ratio / 10000;
      let data = {
        configKey: 'bonus.allot.time',
        configValue: JSON.stringify(dataFrom),
      }
      changeConfig(data).then(res => {
        if(res.code == 200){
          this.$modal.msgSuccess("修改配置成功！");
        }
      })
    },

    /** 查询列表 */
    getList() {
      this.loading = true;
      productAreaList(this.queryParams).then((response) => {
        this.areaList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品区域";
    },
    /** 修改按钮操作 */
    handleUpdate:Debounce(function (row) {
      this.reset();
      detailArea(row.areaId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品区域";
      });
    },500),
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 删除按钮操作 */
    handleDelete:Debounce(function (row) {
      this.$modal.confirm('是否确认删除名称为"' + row.areaName + '"的数据项？').then(function() {
        return delArea(row.areaId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },500),
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm:Debounce(function (row) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let data = {
            areaName: this.form.areaName,
          };
          if (this.form.areaId) {
            data.areaId = this.form.areaId;
            modifyArea(data).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertArea(data).then((response) => {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },500),
  },
};
</script>